package com.ettractions.kiosk.models
{
	import com.ettractions.kiosk.events.BrochureSelectEvent;
	import com.ettractions.kiosk.vo.BrochureVO;
	import com.ettractions.kiosk.vo.CategoryVO;
	import com.ettractions.kiosk.vo.KioskVO;
	
	import flash.utils.Dictionary;
	
	import org.robotlegs.mvcs.Actor;

	/**
	 * @author Scott Steven Rodriguez
	 */
	public class KioskModel extends Actor
	{
		public var brochures:Vector.<BrochureVO>;
		
		private var _data:KioskVO;
		
		private var _selectedBrochureIndex:int = NaN;
		
		private var _categoryIndeces:Dictionary = new Dictionary();
		
		public function set data(d:KioskVO):void
		{
			var brochuresByCategory:Dictionary = new Dictionary();
			var category:CategoryVO;
			var brochure:BrochureVO;
			
			_data = d;
			
			brochures = new Vector.<BrochureVO>();
			
			for each (category in _data.categories)
			{
				brochuresByCategory[category] = new Vector.<BrochureVO>();
				
				for each (brochure in _data.brochures)
					if (brochure.categories.indexOf(category) > -1)
						brochuresByCategory[category].push(brochure);
			}
			
			for each (category in _data.categories)
			{
				_categoryIndeces[category] = brochures.length;
				
				for each (brochure in brochuresByCategory[category])
					brochures.push(brochure);
			}
		}
		
		public function get data():KioskVO
		{
			return _data;
		}
		
		public function get selectedBrochure():BrochureVO
		{
			if (selectedBrochureIndex > -1)
				return brochures[selectedBrochureIndex];
			
			return null;
		}
		
		public function set selectedBrochureIndex(index:int):void
		{
			if (_selectedBrochureIndex != index)
			{
				var brochure:BrochureVO = null;
				var category:CategoryVO = null;
				
				_selectedBrochureIndex = index;
				
				if (index > -1)
				{
					brochure = brochures[index];
					
					for each (var categoryVO:CategoryVO in _data.categories)
						if (index >= _categoryIndeces[categoryVO])
							category = categoryVO;
				}
				
				eventDispatcher.dispatchEvent(new BrochureSelectEvent(BrochureSelectEvent.BROCHURE_SELECTED, index, brochure, category));
			}
		}
		
		public function get selectedBrochureIndex():int
		{
			return _selectedBrochureIndex;
		}
		
		public function offsetBrochureIndex(offset:int):void
		{
			if (offset != 0)
			{
				var newIndex:int = selectedBrochureIndex + offset;
				
				if (newIndex < 0)
					newIndex = brochures.length + newIndex;
				
				else if (newIndex >= brochures.length)
					newIndex = newIndex - brochures.length;
				
				selectedBrochureIndex = newIndex;
			}
		}
				
		public function selectBrochure(brochure:BrochureVO, category:CategoryVO = null):void
		{
			if (brochure == null)
				selectedBrochureIndex = -1;
			
			else if (category == null
			|| brochure.categories.indexOf(category) == -1)
				selectedBrochureIndex = brochures.indexOf(brochure);
			
			else
			{
				// TO DO
			}
		}
		
		public function selectFirstBrochureInCategory(category:CategoryVO):void
		{
			selectedBrochureIndex = _categoryIndeces[category];
		}
	}
}